package com.tim.android.widget.badge;

import com.tim.android.utils.ComputeTool;
import com.tim.android.widget.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class BadgeView extends FrameLayout {
	
	private ImageView mBodyIcon;
	private ImageView mBadgeIcon;
	
	private Context mContext;
	
	private Animation mShowAnimation;
	private Animation mDismissAnimation;
	
	private LayoutParams mBodyLayoutParams;
	private LayoutParams mBadgeLayoutParams;
	
	private int mBadgeGravity;
	
	public BadgeView(Context context) {
		super(context);
		this.mContext = context;
		this.mBodyIcon = new ImageView(mContext);
		this.mBadgeIcon = new ImageView(mContext);
		this.mBadgeGravity = Gravity.LEFT;
		initComponent();
	}

	public BadgeView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	public BadgeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.mContext = context;
		TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.Badge);
		Drawable body = a.getDrawable(R.styleable.Badge_body_icon);
		Drawable badge = a.getDrawable(R.styleable.Badge_badge_icon);
		mBadgeGravity = a.getInt(R.styleable.Badge_badge_gravity, Gravity.LEFT);
		if(body == null || badge == null) {
			throw new IllegalArgumentException("Please both set body icon and badge icon in BadgeView !!");
		}
		a.recycle();
		mBodyIcon = new ImageView(mContext);
		mBadgeIcon = new ImageView(mContext);
		mBodyIcon.setBackgroundDrawable(body);
		mBadgeIcon.setBackgroundDrawable(badge);
		initComponent();
	}
	
	private void initComponent() {
		mBodyLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		mBadgeLayoutParams = new LayoutParams(
								(int)ComputeTool.dip2px(mContext, 25), 
								(int)ComputeTool.dip2px(mContext, 25), 
								mBadgeGravity);
		int margin = (int) ComputeTool.dip2px(mContext, 5);
		mBodyLayoutParams.setMargins(margin, margin, margin, margin);
		mBodyIcon.setLayoutParams(mBodyLayoutParams);
		mBadgeIcon.setLayoutParams(mBadgeLayoutParams);
		mBadgeIcon.setVisibility(View.INVISIBLE);
		addView(mBodyIcon);
		addView(mBadgeIcon);
	}

	public void showBadge() {
		if(!isShowingBadge()) {
			if(mShowAnimation != null) {
				mBadgeIcon.startAnimation(mShowAnimation);
			}
			mBadgeIcon.setVisibility(View.VISIBLE);
		}
	}
	
	public void dismissBadge() {
		if(isShowingBadge()) {
			if(mShowAnimation != null) {
				mShowAnimation.cancel();
				mBadgeIcon.clearAnimation();
			}
			if(mDismissAnimation != null) {
				mBadgeIcon.startAnimation(mDismissAnimation);
			}
			mBadgeIcon.setVisibility(View.INVISIBLE);
		}
	}
	
	public boolean isShowingBadge() {
		return mBadgeIcon.getVisibility() == View.VISIBLE;
	}
	
	public void setBodyDrawable(Drawable body) {
		this.mBodyIcon.setBackgroundDrawable(body);
	}
	
	public void setBadgeDrawable(Drawable badge) {
		this.mBadgeIcon.setBackgroundDrawable(badge);
	}

	public Animation getShowAnimation() {
		return mShowAnimation;
	}

	public void setShowAnimation(Animation showAnimation) {
		this.mShowAnimation = showAnimation;
	}

	public Animation getDismissAnimation() {
		return mDismissAnimation;
	}

	public void setDismissAnimation(Animation dismissAnimation) {
		this.mDismissAnimation = dismissAnimation;
	}

	public int getBadgeGravity() {
		return mBadgeLayoutParams.gravity;
	}

	public void setBadgeGravity(int badgeGravity) {
		this.mBadgeGravity = badgeGravity;
		this.mBadgeLayoutParams.gravity = mBadgeGravity;
		this.mBadgeIcon.setLayoutParams(mBadgeLayoutParams);
	}
}
